require(['./config'], () => {
  require(['template', 'request', 'header', 'footer', 'zoom', 'fly','cookie'], (template, request, header) => {
    class Detail {
      constructor() {
        // console.dir($)
        this.init()
        this.addToCart()
        this.baynow()
      }
      init () {
        const id = window.location.search.slice(4)
        request.getDetail(id).then(resp => {
          if (resp.code === 200) {
            // const nickName=resp.data.shop.title;
            const { detail } = resp.data;
            console.log(detail);
            const {
              id,
              title,
              price,
              photo,
              saleNum,
              descContentList,
              accessoryHints,
              shop,
              tbOriginPrice
            } = detail;
            this.detail = {
              id,
              title,
              price,
              photo,
              saleNum,
              descContentList,
              accessoryHints,
              shop,
              tbOriginPrice
            };
            $('#design').html(template('src_design', {
              ...this.detail
            }));
            this.zoom()
            this.changeNum()

          }
        })
      }
      zoom () {
        $('.big-img').elevateZoom({
          gallery: 'gal1'
        })
      }
      changeNum () {
        var shopNum = 1;
        //加数量
        $('#design').on('click', '.btn-add-tocar', function () {
          shopNum++;
          $('#shop-number').attr("value", $('#shop-number').val(shopNum));

        })
        //减数量
        $('#design').on('click', '.btn-sub-tocar', function () {
          if (shopNum > 1) {
            shopNum--;
            $('#shop-number').attr("value", $('#shop-number').val(shopNum));
            // console.log(shopNum);
          }
        })
      }

      addToCart () {
        $('#design').on('click', '#add-to-cart-btn', e => {
          this.fly(e)

          let cartList = localStorage.getItem('cart')
          if (cartList) {
            cartList = JSON.parse(cartList)
            // 判断已经存过的数据里是否有当前商品
            const isExist = cartList.some(cart => {
              return cart.id === this.detail.id
            })
            if (isExist) {
              //代表有相同的数据
              console.log(888)
              cartList = cartList.map(cart => {
                if (cart.id == this.detail.id) cart.num++
                return cart
              })

            } else {
              cartList.push({
                id: this.detail.id,
                title: this.detail.title,
                price: this.detail.price,
                photo: this.detail.photo[0],
                //商品数量
                num: parseInt($('#shop-number').val()),
                check: true

              })
            }
            localStorage.setItem('cart', JSON.stringify(cartList))
          } else {
            //第一次存
            localStorage.setItem('cart', JSON.stringify([{
              id: this.detail.id,
              title: this.detail.title,
              price: this.detail.price,
              photo: this.detail.photo[0],
              //商品数量
              num: parseInt($('#shop-number').val()),
              check: true

            }]))
            // console.log(2)
          }
        })
      }

      baynow(){
        $('#design').on('click','.buy',function(){
          let info = $.cookie('userInfo')
          if(info){
            window.location.href = '/html/payment.html'
          }else{
            alert('请先登录，即将跳转到登录页面')
            window.location.href = '/html/login.html'
          }
        })
      }
      fly (e) {
        const _this = this
        $(`<img src="${this.detail.photo[0].url}" style="width: 30px; height: 30px;">`).fly({
          start: {
            left: e.clientX,  //开始位置（必填）#fly元素会被设置成position: fixed
            top: e.clientY,  //开始位置（必填）
          },
          end: {
            left: $('#car').offset().left, //结束位置（必填）
            top: $('#car').offset().top - $(window).scrollTop(),  //结束位置（必填）,
            width: 0,
            height: 0
          },
          speed: 1.2,
          vertex_Rtop: 200,
          onEnd: function () {
            this.destroy()
            header.calcCartNum()

          }
        })
      }
    }
    new Detail()
  })
})